﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ShopBack.Web.Controllers
{
    [Authorize]
    public class CustMoerController : Controller
    {
        // GET: CustMoer
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 客户信息
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public ActionResult GetCustMoer(int limit, int page, string CustName,string Sex)
        {
            var total = 0;
            var list = CustomerService.GetUserMes(limit, page, CustName, Sex ,out total).ToList();
            return Json(new { code = 0, msg = "", data = list, count = total }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <returns></returns>
        public ActionResult BatchDelArticele(string Ids)
        {
            int a = CustomerService.GetBatchDelByIds(Ids);
            if (a > 0)
            {
                return Json(new { Success1 = true, Message = "删除成功" });
            }
            else
            {
                return Json(new { Success1 = false, Message = "删除失败" });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public ActionResult Del(int id)
        {
            int a = CustomerService.GetDel(id);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "删除成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "删除失败" });
            }
        }
        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Update(int id)
        {
            Customer customer = CustomerService.GetSelec(id);
            return View(customer);
        }
        /// <summary>
        /// 编辑数据
        /// </summary>
        /// <param name="CustName"></param>
        /// <param name="Sex"></param>
        /// <param name="Birthday"></param>
        /// <param name="Phone"></param>
        /// <param name="realId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Update(string CustName, string Sex, string Birthday, string Phone, int realId)
        {
            int a = CustomerService.GetUpdate(CustName, Sex, Birthday, Phone, realId);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "修改成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "修改失败" });
            }
        }
        /// <summary>
        /// 修改密码页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult UpdatePwd(int id)
        {
            Customer a = CustomerService.GetSelec(id);
            return View(a);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="PassWord">旧密码</param>
        /// <param name="NewPassWord">新密码</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdatePwd(string CustName, string Pwd, string NewPwd)
        {

            Customer u = CustomerService.GetUser(CustName);
            string oldpwd = MD5Jm.Md5jm(Pwd);
            string newPwd = MD5Jm.Md5jm(NewPwd);
            if (u.Pwd == oldpwd)
            {
                if (string.IsNullOrEmpty(NewPwd))
                {
                    return Json(new { Success = false, Message = "新密码不能为空" });
                }
                else if (oldpwd == newPwd)
                {
                    return Json(new { Success = false, Message = "新密码和原密码不能一致" });
                }
                else
                {
                    int a = CustomerService.GetUpdatePwd(CustName, newPwd);
                    if (a > 0)
                    {
                        return Json(new { Success = true, Message = "修改成功" });
                    }
                    else
                    {
                        return Json(new { Success = false, Message = "修改失败" });
                    }
                }
            }
            else
            {
                return Json(new { Success = false, Message = "原密码密码不一致" });
            }


        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public ActionResult RestPwd(int id)
        {

            Customer b = CustomerService.GetSelec(id);
            if (b.Pwd == "e10adc3949ba59abbe56e057f20f883e")
            {
                return Json(new { Success = true, Message = "修改成功,原密码：123456" });
            }
            else
            {
                int a = CustomerService.GetRestPwd(id);
                if (a > 0)
                {
                    return Json(new { Success = true, Message = "修改成功,原密码：123456" });
                }
                else
                {
                    return Json(new { Success = false, Message = "修改失败" });
                }
            }

        }
    }
}